package main

// 使用golang, 编写连接nacos的终端

import (
	"fmt"

	"github.com/nacos-group/nacos-sdk-go/clients"
	"github.com/nacos-group/nacos-sdk-go/common/constant"
	"github.com/nacos-group/nacos-sdk-go/vo"
)

func main() {
	//create clientConfig
	clientConfig := constant.ClientConfig{
		NamespaceId:         "4e5976d4-18f7-4ed3-9cab-a74b9c29a843", //we can create multiple clients with different namespaceId to support multiple namespace.When namespace is public, fill in the blank string here.
		TimeoutMs:           5000,
		Username:            "USER_CN",
		Password:            "Yz92DngyGtKFAN7ZVeha",
		NotLoadCacheAtStart: true,
		LogDir:              "/tmp/nacos/log",
		CacheDir:            "/tmp/nacos/cache",
		LogLevel:            "debug",
	}
	// At least one ServerConfig
	serverConfigs := []constant.ServerConfig{
		{
			IpAddr:      "dc03.iotdreamcatcher.net.cn",
			ContextPath: "/nacos",
			Port:        8848,
			Scheme:      "http",
		},
	}

	// Create config client for dynamic configuration
	_, _ = clients.CreateConfigClient(map[string]interface{}{
		"serverConfigs": serverConfigs,
		"clientConfig":  clientConfig,
	})

	// Another way of create config client for dynamic configuration (recommend)
	configClient, err := clients.NewConfigClient(
		vo.NacosClientParam{
			ClientConfig:  &clientConfig,
			ServerConfigs: serverConfigs,
		},
	)

	// 获取配置
	content, err := configClient.GetConfig(vo.ConfigParam{
		DataId: "dcv2.product.skills",
		Group:  "COMMON",
	})

	if err != nil {
		panic(err)
	}

	// 打印配置内容
	fmt.Println(content)
}
